home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-27 | 4.5 KB | 207 lines | [TEXT/KAHL] |
- //----------------------------------------------------------------------------------
- // File : movableModal.c
- // Date : April 4, 1994
- // Author : Jim Stout
- // Purpose : Implements movableModal dialogs
- //------------------------------------------------------------------------------------
- #include <Palettes.h>
- #include "movableModal.h"
-
- #define CANCEL_ITEM_IS_2 1 // if dialog item 2 is "Cancel" button
-
- // following must be in your application code!!
- // or remove the calls from this code...
-
- extern void doUpdate (EventRecord * theEvent);
- extern void doPeriodicEvent (EventRecord * theEvent);
- extern void doMenuCommand (long menuResult);
-
- pascal void movableModalDialog(ModalFilterUPP filter, short *theItem)
- {
- EventRecord theEvent;
- DialogPtr d,thisDialog;
- GrafPtr oldPort;
-
- thisDialog = FrontWindow();
- GetPort(&oldPort);
- SetPort(thisDialog);
-
- for(;;) {
- WaitNextEvent(everyEvent, &theEvent, 20, 0L);
- if( (*theItem = preFilterEvent(thisDialog, &theEvent)) != 0)
- break;
-
- if (filter != nil)
- if (CallModalFilterProc(filter, thisDialog, &theEvent, theItem))
- break;
-
- if (IsDialogEvent(&theEvent))
- if (DialogSelect(&theEvent, &d, theItem))
- break;
-
- doPeriodicEvent(&theEvent);
- }
- SetPort(oldPort);
- }
-
- static short preFilterEvent(DialogPtr d, EventRecord *theEvent)
- {
- char key;
- short ret=0,defItem,t;
- long ticks;
- Handle h;
- Rect r;
- PenState ps;
- GDHandle hThisDevice=0;
- RgnHandle c;
- RGBColor saveFore, saveBack={-1,-1,-1}, rgbGray={0,0,0};
- GrafPtr savePort;
-
- switch (theEvent->what) {
- case mouseDown:
- ret = doMouseDialog(d,theEvent); /* handle drag etc. of dialog */
- break;
- case diskEvt:
- diskEvent(theEvent);
- break;
- case updateEvt:
- if(d != (DialogPtr)theEvent->message)
- doUpdate(theEvent);
- else {
- GetPort(&savePort);
- SetPort(d);
-
- BeginUpdate(d);
- EraseRect(&d->portRect);
- UpdtDialog(d, d->visRgn);
- // DrawControls(d);
- EndUpdate(d);
-
- defItem = ((DialogPeek)d)->aDefItem;
- if(defItem) {
- c = NewRgn();
- GetClip(c);
- GetDItem(d, defItem, &t, &h, &r);
- if((**(ControlHandle)h).contrlHilite == 0xFF) {
- if(hasGrayText()) {
- GetForeColor(&saveFore);
- hThisDevice = GetGDevice();
- if(GetGray(hThisDevice, &saveBack, &rgbGray))
- RGBForeColor(&rgbGray);
- }
- }
- GetPenState(&ps);
- PenSize(3,3);
- PenPat(&qd.black);
- InsetRect(&r, -4, -4);
- ClipRect(&r);
- FrameRoundRect(&r, 16, 16);
- SetPenState(&ps);
- if(hThisDevice)
- RGBForeColor(&saveFore);
- SetClip(c);
- DisposeRgn(c);
- }
- SetPort(savePort);
- }
- break;
-
- case keyDown:
- case autoKey:
- defItem = ((DialogPeek)d)->aDefItem;
- if(!defItem)
- break;
- key = (theEvent->message & charCodeMask);
-
- if (key == _RETURNKEY || key == _ENTRKEY){
- GetDItem(d, defItem, &t, &h, &r);
- if((**(ControlHandle)h).contrlHilite != 0xFF)
- ret = defItem;
- }
- #if CANCEL_ITEM_IS_2
- else
- if (key == _ESCAPEKEY || (key == _PERIODKEY &&
- theEvent->modifiers & cmdKey) ) {
- GetDItem(d, 2, &t, &h, &r);
- ret = 2;
- }
- #endif
- if(h && ret) {
- HiliteControl((ControlHandle)h, inButton);
- Delay(8,&ticks);
- HiliteControl((ControlHandle)h, 0);
- }
- break;
- }
- return (ret);
- }
-
- static short doMouseDialog(DialogPtr d, EventRecord *theEvent)
- {
- WindowPtr theWindow;
- short partCode, ret=0;
-
- switch (partCode = FindWindow(theEvent->where,&theWindow)) {
- case inDrag:
- if(theWindow == d) {
- DragWindow(d, theEvent->where, &qd.screenBits.bounds);
- theEvent->what = nullEvent;
- }
- break;
-
- case inMenuBar:
- doMenuCommand(MenuSelect(theEvent->where));
- break;
-
- case inGoAway:
- if (TrackBox (theWindow, theEvent->where, partCode)) {
- ret = cancel;
- theEvent->what = nullEvent;
- }
- break;
-
- /* add code if you need to deal with these mouseDown events… */
-
- case inGrow:
- break;
- case inZoomIn:
- case inZoomOut:
- break;
- case inContent:
- if(theWindow != d) {
- SysBeep(1);
- }
- break;
- default:
- break;
- }
- return(ret);
- }
-
- extern void diskEvent(EventRecord *theEvent)
- {
- Point diskInitPt;
-
- if (HiWord (theEvent->message) != noErr) {
- diskInitPt.v = 120;
- diskInitPt.h = 100;
- DILoad ();
- (void) DIBadMount (diskInitPt, theEvent->message);
- DIUnload ();
- theEvent->what = nullEvent;
- }
- }
- static Boolean hasGrayText (void)
- {
- OSErr err;
- short bit = gestaltHasGrayishTextOr;
- long gResult;
- Boolean result = false;
-
- err = Gestalt(gestaltQuickdrawFeatures,&gResult);
- if(err == noErr) {
- if(BitTst(&gResult, 31-bit))
- result = true;
- }
- return(result);
- }